package cn.xshi.common.base;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import net.sf.json.JSONArray;

/**
 * @Desc BaseBTreeGridEntity
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Data
public class BaseBTreeGridEntity{
	private String id;/**ID编号**/
	private String pid;/**父级编号**/
	private String text;/**显示值**/
	private String icon;/**图片**/
	private String qtip;/**提示**/
	private Boolean leaf;/**叶子**/
	private String tempObject;/**临时值**/
	private String content;/**描述**/
	private String integerappend;/**int类型拼接使用可以使用@或逗号隔开**/
	private String buessid;/**业务id**/
	private ArrayList<BaseBTreeGridEntity> children = new ArrayList<BaseBTreeGridEntity>();/**子节点集合**/
	private Long idConvertInteger;/**将id为非整数类型动态转换为整型字段**/
	private Long pidConvertInteger;/**将pid为非整数类型动态转换为整型字段**/
	private Integer nodeLevel = 1;/**深度 默认1**/
	private String vueIcon;/**VUE字体图标**/

	/**
	 * 返回JSON字符串
	 * @param list
	 * @return
	 */
	public static String buildTree(List<BaseBTreeGridEntity> list){  
		List<BaseBTreeGridEntity> baseBTreeGridEntities = new ArrayList<BaseBTreeGridEntity>();
        for(int i = 0; i < list.size(); i++) { 
        	BaseBTreeGridEntity node = list.get(i);
            if ("0".equals(node.getPid()) || StringUtils.isEmpty(node.getPid())) {  
            	node.setIdConvertInteger(new Long(i+1));//设置id转换器
            	node.setBuessid(node.getId());
            	node.setPid("0");
            	node.setPidConvertInteger(new Long(0));//设置pid转换器
				baseBTreeGridEntities.addAll(build(list,node));
				baseBTreeGridEntities.add(node);
            }
        }  
        
        //统一转换
        JSONArray jsonArray = new JSONArray();
        for(BaseBTreeGridEntity baseBTreeGridEntity:baseBTreeGridEntities){
        	Map<String, Object> map = new HashMap<String,Object>();
        	map.put("id", baseBTreeGridEntity.getIdConvertInteger());
        	map.put("pid", baseBTreeGridEntity.getPidConvertInteger());
        	map.put("name", baseBTreeGridEntity.getText());
        	map.put("tempObject", baseBTreeGridEntity.getTempObject());
        	map.put("buessid", baseBTreeGridEntity.getBuessid());
			map.put("nodeLevel", baseBTreeGridEntity.getNodeLevel());
			map.put("leaf",baseBTreeGridEntity.leaf);
			map.put("vueIcon",baseBTreeGridEntity.getVueIcon());
        	map.put("content", StringUtils.isEmpty(baseBTreeGridEntity.getContent())?"":baseBTreeGridEntity.getContent());
        	map.put("integerappend", StringUtils.isEmpty(baseBTreeGridEntity.getIntegerappend())?"":baseBTreeGridEntity.getIntegerappend());
        	jsonArray.add(map);
        }
        return  jsonArray.toString();
    } 
	/**
	 * 返回JSON字符串
	 * @param list
	 * @return
	 */
	public static String buildTreeF(List<BaseBTreeGridEntity> list){  
		List<BaseBTreeGridEntity> baseBTreeGridEntities = new ArrayList<BaseBTreeGridEntity>();
        for(int i = 0; i < list.size(); i++) { 
        	BaseBTreeGridEntity node = list.get(i);
            if ("0".equals(node.getPid()) || StringUtils.isEmpty(node.getPid())) {  
            	node.setIdConvertInteger(new Long(i+1));//设置id转换器
            	node.setBuessid(node.getId());
            	node.setPid("0");
            	node.setPidConvertInteger(new Long(0));//设置pid转换器
				baseBTreeGridEntities.addAll(build(list,node));
				baseBTreeGridEntities.add(node);
            }
        }  
        
        //统一转换
        JSONArray jsonArray = new JSONArray();
        for(BaseBTreeGridEntity baseBTreeGridEntity:baseBTreeGridEntities){
        	Map<String, Object> map = new HashMap<String,Object>();
        	map.put("id", baseBTreeGridEntity.getIdConvertInteger());
        	map.put("parentId", baseBTreeGridEntity.getPidConvertInteger() == 0?null:baseBTreeGridEntity.getPidConvertInteger());
        	map.put("name", baseBTreeGridEntity.getText());
        	map.put("tempObject", baseBTreeGridEntity.getTempObject());
        	map.put("buessid", baseBTreeGridEntity.getBuessid());
			map.put("nodeLevel", baseBTreeGridEntity.getNodeLevel());
			map.put("leaf",baseBTreeGridEntity.leaf);
			map.put("vueIcon",baseBTreeGridEntity.getVueIcon());
			map.put("content", StringUtils.isEmpty(baseBTreeGridEntity.getContent())?"":baseBTreeGridEntity.getContent());
        	map.put("integerappend", StringUtils.isEmpty(baseBTreeGridEntity.getIntegerappend())?"":baseBTreeGridEntity.getIntegerappend());
        	jsonArray.add(map);
        }
        return  jsonArray.toString();
    }
	/**
	 * 子节点遍历
	 * @param list
	 * @param node
	 * @return
	 */
    private static List<BaseBTreeGridEntity> build(List<BaseBTreeGridEntity> list,BaseBTreeGridEntity node){
    	 List<BaseBTreeGridEntity> children = new ArrayList<BaseBTreeGridEntity>(); 
         int size = list.size();
         for(int i = 0; i < size; i++){
         	BaseBTreeGridEntity child = list.get(i);
         	if (node.getId().equals(child.getPid())) {  
         		child.setBuessid(child.getId());
         		child.setNodeLevel(node.getNodeLevel()+1);
         		child.setPidConvertInteger(node.getIdConvertInteger());//设置pid转换器
         		child.setIdConvertInteger(new Long(node.getIdConvertInteger()+""+(i+1)));//设置id转换器
                children.add(child);  
                children.addAll(build(list, child));
             }
         }
         return children;  
    }  
}
